
layui.use(['form', 'layer', 'laydate', 'table', 'upload'], function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        upload = layui.upload,
        table = layui.table;

    var datalist_url = $('.datalist_url').val();
    var add_url = $('.add_url').val();
    var edit_url = $('.edit_url').val();
    var detail_url = $('.detail_url').val();
    var delete_url = $('.delete_url').val();
    var save_url = $('.save_url').val();

    laydate.render({
        elem: '.lay-date-range',
        type: 'datetime',
        range: '~', // '~' 自定义分割字符
        format: 'yyyy-MM-dd HH:mm:ss',
    });


    // Table 列表
    var tableIns = table.render({
        elem: '#dataTable',
        url : datalist_url,
        page : true,
        cellMinWidth : 95,
        //height : "full-100",
        limit : 10,
        limits : [10, 20, 50, 100],
        id : "tableList",
        initSort: {
            field: 'create_date' // 排序字段，对应 cols 设定的各字段名
            ,type: 'desc' // 排序方式  asc: 升序、desc: 降序、null: 默认排序
        },
        cols : [[
            /* {type: "checkbox", fixed:"left", width:50},*/
            {field: 'order_no', title: '单据编号'},
            {field: 'create_date', title: '创建日期', align:'center',sort: true},
            {field: 'pay_type', title: '付款方式', align:'center'},
            {field: 'pay_reality', title: '单据金额',align:"center"},
            {field: 'total', title: '实付金额', align:'center'},
            {field: 'discount_price', title: '优惠金额', align:'center'},
            {field: 'freight', title: '配送费', align:'center'},
            {field: 'sum_integral', title: '积分金额', align:'center'},
            {field: 'sum_integral', title: '赠送积分', align:'center'},
            {field: 'account_number', title: '会员号', align:'center'},
            {field: 'my_collect_id', title: '促销', align:'center'},
            {field: 'show_deliver_method', title: '配送/自提', align:'center'},
            {field: 'status', title: '状态', align:'center'},
            {title: '操作', fixed:"right", align:"center",templet:function(data){
                    var html = '';
                    html += '<a type="button" class="layui-btn layui-btn-xs layui-btn-normal" href="'+detail_url+'/id/'+data.id+'"><i class="layui-icon">&#xe60a;</i>详情</a>';
                    /*html += '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="detail"><i class="layui-icon layui-icon-edit"></i>详情</a>';*/
                    /*html += '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>';*/

                    return html;
                }}
        ]]
    });

    // 搜索
    $(".search_btn").on("click", function(){
        if ($('.order_no').val() != ''){
            var order_no = $('.order_no').val();
        }
        if ($('.account_number').val() != '') {
            var account_number = $('.account_number').val();
        }
        if ($('.deliver_method').val() != '') {
            var deliver_method = $('.deliver_method').val();
        }
        if ($('.status').val() != '') {
            var status = $('.status').val();
        }
        if ($('.lay-date-range').val() != '') {
            var range_date = $('.lay-date-range').val();
        }
        table.reload("tableList", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                order_no: order_no,
                account_number: account_number,
                deliver_method: deliver_method,
                status: status,
                range_date: range_date,
            }
        });
    });

    // 清空搜索
    $(".reset_btn").on("click", function(){
        table.reload("tableList", {
            where: {
                order_no: '',
                account_number: '',
                deliver_method: '',
                status: '',
                range_date: '',
            }
        });
    });

    // 批量删除
    $(".delAll_btn").on("click", function(){
        var checkStatus = table.checkStatus('tableList'),
            data = checkStatus.data,
            ids = [];
        if(data.length > 0) {
            for (var i in data) {
                ids.push(data[i].id);
            }
            layer.confirm('确定删除选中的数据？', {icon: 3, title: '提示信息'}, function (index) {
                $.ajax({
                    url: delete_url,
                    data: {"ids": ids},
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg('删除成功', {icon: 1});
                            tableIns.reload();
                            layer.close(index);
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器无响应");
                    }
                });
            })
        }else{
            layer.msg("请选择需要删除的数据");
        }
    });

    $('.add_btn').on('click', function(){
        formHtml();
    });

    form.on('select(type)', function(data){
        var type = data.value;
        if (type == 1 || type == 2 || type == 3) {
            $('.amount').removeClass("layui-hide");
            $('.strength').addClass("layui-hide");
        }else{
            $('.strength').removeClass("layui-hide");
            $('.amount').addClass("layui-hide");
        }
    });


    // 列表操作
    table.on('tool(dataTable)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        // 删除
        if (layEvent === 'del') {
            layer.confirm('确定删除此数据？',{icon:3, title:'提示信息'},function(index){
                $.ajax({
                    url: delete_url,
                    data: {"id": data.id},
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg('已删除', {icon: 1, time: 1000});
                            tableIns.reload();
                            layer.close(index);
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器无响应");
                    }
                });
            });
        } else if(layEvent === 'detail'){
            /*layer.open({ type: 1,
                //此处以iframe举例
                area: ['690px', '350px'],
                area: ['750px', '75%'],
                shade: 0.5,
                maxmin: true,
                //url: detail_url + '?id=' + data.id
                content: ['detail.html','yes'],
            });*/

            /*location.href = detail_url + '?id=' + data.id*/
        }
    });

    // Form 表单
    function formHtml(data){
        var content = detail_url;

        if(data){
            content = detail_url + '?id=' + data.id;
        }
        // 右侧面板
        var index = layer.open({
            type: 2,
            anim: -1,
            title: false,
            closeBtn: false,
            offset: 'r',
            shade: 0.2,
            shadeClose: true, // 是否点击遮罩关闭
            skin: 'layui-anim layui-anim-rl layui-layer-adminRight',
            area: '1200px',
            top: 0,
            content: content
        });
    }

    // 文件上传
    upload.render({
        elem: '.linkLogo',
        url: '../../json/linkLogo.json',
        method : "post",
        done: function(res, index, upload){
            var num = parseInt(4*Math.random());  //生成0-4的随机数，随机显示一个头像信息
            $('.linkLogoImg').attr('src',res.data[num].src);
            $('.linkLogo').css("background","#fff");
        }
    });

    // 表单提交
    form.on("submit(laySave)", function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        $.ajax({
            url: save_url,
            data: data.field,
            type: "post",
            dataType: "json",
            success: function(res){
                top.layer.close(index);
                top.layer.msg(res.msg);
                if(res.code == 1){
                    // var index = parent.layer.getFrameIndex(window.name);
                    // // 关闭当前frame
                    // parent.layer.close(index);

                    layer.closeAll("iframe");
                    //parent.window.tableIns.reload();
                    parent.layui.table.reload('tableList', {});
                    // 刷新父页面
                    //parent.location.reload();
                }
            },
            error:function(data){
                layer.msg("服务器无响应");
            }
        });

    })
});